package com.ruoyi.autoTask;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.inducePest.service.inducePest.InducePestCaptureImgService;
import com.ruoyi.lightkillPest.controller.device.SCDDeviceController;
import com.ruoyi.permission.domain.BaseBlockPermiDeviceInfo;
import com.ruoyi.permission.domain.BlockPermiDeviceInfo;
import com.ruoyi.permission.service.PermiService;
import com.ruoyi.web.mapper.PermitMapper;
import com.ruoyi.xag.controller.IotController;
import com.ruoyi.xag.service.DeviceService;
import com.ruoyi.xag.ysty.controller.YSTYDeviceController;
import com.ruoyi.xag.ysty.domain.BusinessAndDeviceId;
import com.ruoyi.xag.ysty.domain.YSTYDeviceInfo;
import com.ruoyi.xag.ysty.service.YSTYDeviceService;
import com.ruoyi.ysVideo.controller.EzvizController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 设置定时任务
 *
 * @author Mr-Le
 */
@Component
@Configuration
@EnableScheduling
public class AutoTaskTest  {

    @Autowired
    private YSTYDeviceService ystyDeviceService;
    @Autowired
    private PermiService permiService;

    /**
     * 视频监控
     * 每小时更新一次球机的状态
     */
    @Scheduled(cron = "0 0 */1 * * ?")
    private void refreshCameraStatus(){
        EzvizController ezvizController = SpringUtils.getBean(EzvizController.class);
//        获取所有在权限表中的设备，并筛选出视频监控的设备进行状态更新
        List<String> ezvizList = new ArrayList<>();
        List<BaseBlockPermiDeviceInfo> allDeviceInfoList = permiService.selectAllDeviceByUnitId(0);
        for(BaseBlockPermiDeviceInfo baseBlockPermiDeviceInfo:allDeviceInfoList){
            if(baseBlockPermiDeviceInfo.getTypeId() == 6){
                ezvizList.add(baseBlockPermiDeviceInfo.getDeviceSerial());
            }
        }
        for(String deviceSerial:ezvizList){
            ezvizController.getDetailInfoByDeviceSerial(deviceSerial);
        }
//        ezvizController.getDetailInfoByDeviceSerial("F94404261");
//        ezvizController.getDetailInfoByDeviceSerial(    "G45812905");
//        ezvizController.getDetailInfoByDeviceSerial("G45812906");
//        ezvizController.getDetailInfoByDeviceSerial("AD8746100");
//        ezvizController.getDetailInfoByDeviceSerial("K19666298");
    }

    /**
     * 性诱捕器
     * 每隔一小时从oss拉取图片
     */
    @Scheduled(cron = "0 0 */1 * * ? ")
    private void inducePset() throws ParseException {

        System.out.println("开始从OSS库拉取图片");
        System.out.println(new Date());
        InducePestCaptureImgService inducePestCaptureImgService = SpringUtils.getBean(InducePestCaptureImgService.class);
        //InducePestCaptureImgServiceImpl inducePestCaptureImgService = new InducePestCaptureImgServiceImpl();
        inducePestCaptureImgService.downImageFromOSSAuto();

        System.out.println("执行完成");
    }

    /**
     * 杀虫灯
     * 每24小时更新状态
     */
    @Scheduled(cron = "0 0 */24 * * ? ")
    public void SCDStatus(){
        System.out.println("每24小时更新所有杀虫灯的状态");
        SCDDeviceController scdDeviceController = SpringUtils.getBean(SCDDeviceController.class);
        AjaxResult result = scdDeviceController.updateDeviceStatus();
        System.out.println(result);
    }
    /**
     * 气象土壤墒情设备
     * 物联网设备更新数据
     */
    @Scheduled(cron = "0 0 0/1 * * ? ")
    private void iotDevice() {
        System.out.println("开始执行物联网设备信息更新");
//        存储云上田野气象设备
        List<BusinessAndDeviceId> ystyWeatherList = new ArrayList<>();
//        存储云上田野土壤设备
        List<BusinessAndDeviceId> ystySoilList = new ArrayList<>();

        List<YSTYDeviceInfo> ystyDeviceInfoList = ystyDeviceService.selectAllYSTYDevice();
        for(YSTYDeviceInfo ystyDeviceInfo:ystyDeviceInfoList){
            if(ystyDeviceInfo.getDeviceName().startsWith("W")){
                String soilId = ystyDeviceInfo.getDeviceId();
                String business = ystyDeviceInfo.getBusiness();
                ystySoilList.add(new BusinessAndDeviceId(soilId,business));
            }else {
                String weatherId = ystyDeviceInfo.getDeviceId();
                String business = ystyDeviceInfo.getBusiness();
                ystyWeatherList.add(new BusinessAndDeviceId(weatherId,business));
            }
        }

        IotController iotController = SpringUtils.getBean(IotController.class);
        YSTYDeviceController ystyDeviceController = SpringUtils.getBean(YSTYDeviceController.class);

        AjaxResult result = iotController.dataPointsLast("W863320045453949","W863320045456603");
        AjaxResult result2 = iotController.dataPointsLast("W863320045456181","W863320045462676");
        //云上田野的土壤设备
        for(BusinessAndDeviceId soilDevice:ystySoilList){
            ystyDeviceController.dataInsert(soilDevice);
        }
//        AjaxResult result3 = ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1100314732720173056").setBusiness("1684289043406"));
//        AjaxResult result4 =ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1100314672229920768").setBusiness("1684289043406"));
//        AjaxResult result5 = ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1100314615371935744").setBusiness("1684289043406"));
//        AjaxResult result6 = ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1100314561785507840").setBusiness("1684289043406"));
//        AjaxResult result7 = ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1100314458886647808").setBusiness("1684289043406"));
//        AjaxResult result01 = ystyDeviceController.dataInsert(new BusinessAndDeviceId().setDeviceId("1087756570884452352").setBusiness("1623745496173"));
//        //云上田野的气象站
        for(BusinessAndDeviceId weatherDevice:ystyWeatherList){
            ystyDeviceController.dataInsert(weatherDevice);
        }
//        AjaxResult result8 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("1105809793327181824").setBusiness("1684289043406"));
//        AjaxResult result9 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("1105818498227646464").setBusiness("1684289043406"));
//        AjaxResult result10 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("1105822556451647488").setBusiness("1684289043406"));
//        AjaxResult result11 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("1105824882688143360").setBusiness("1684289043406"));
//
//        AjaxResult result12 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("806843316183773184").setBusiness("1623745496173"));
//        AjaxResult result13 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("806840914143621120").setBusiness("1623745496173"));
//        AjaxResult result14 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("806831883513507840").setBusiness("1623745496173"));
//        AjaxResult result15 = ystyDeviceController.weatherInsert(new BusinessAndDeviceId().setDeviceId("806513667104845824").setBusiness("1623745496173"))
        //极飞设备拉取
        AjaxResult result0 = iotController.list();
        // 执行情况说明
        System.out.println("极飞气象土壤情况1：" + result);
        System.out.println("极飞气象土壤情况2：" + result2);
        System.out.println("极飞设备获取情况：" + result0);
    }



    /**
     * 设置某一时间 此前某小时 的 date
     * 返回类型为 Date： return c.getTime();
     * String： return sdf.format(c.getTime());
     *
     * @param date  字符串类型的date
     * @param hours 小时
     * @return 本次用到String类型，修改为String
     */
    public static String beforeHourToNowDate(String date, int hours) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            c.setTime(sdf.parse(date));
            c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) - hours);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdf.format(c.getTime());
    }


}
